const {deployments,ethers, getNamedAccounts} = require("hardhat");
const {expect} = require("chai");



describe("NftAuction Contract", function () {

    before(async function () { 
       [deployer,user1] =  await ethers.getSigners();
       
       await deployments.fixture(["nftV1","nftAuctionV1", "nftAuctionV2"]);

       const nft_deployment = await deployments.get("proxyNft");
       this.nft = await ethers.getContractAt("MyNFT", nft_deployment.address);
        console.log("nft address:",await this.nft.target)
       
      
        const deplment = await deployments.get("nftAuctionV2");
        this.nftAuction = await ethers.getContractAt("NftAuction2",deplment.address);
        await this.nftAuction.setPriceFeed("0x1c7d4b196cb0c7b01d743fbc6116a902379c7238","0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43");
        console.log("proxy address:",deplment.address);
        console.log("nftAuction address:",await this.nftAuction.target);

    });
    

    it("upgrade nftAuction",async function(){

       await this.nft.mintNFT(deployer);
       console.log("nft owner:", await this.nft.owner(1));

        await this.nft.setApprovalForAll(this.nftAuction.target, true);


        console.log(await this.nft.isApprovedForAll(deployer, this.nftAuction.target));
     

        const tx = await this.nftAuction.createAuction(1000000, 3, this.nft.target, 1);
        const receipt =await tx.wait();

        const filter = this.nftAuction.filters.CreateAuction();
        const events = await this.nftAuction.queryFilter(filter, receipt.blockNumber, receipt.blockNumber);
     
        const auctionId = events[0].args.auctionId;
        await this.nftAuction.connect(user1).bid(auctionId,0,ethers.ZeroAddress,{value:100000000});

        await this.nftAuction.connect(user2).bid(auctionId,1000,0x1c7d4b196cb0c7b01d743fbc6116a902379c7238);

       await new Promise(async (resolve, reject) => {
            setTimeout(resolve,3*1000);
        })
        //等待10秒

       await this.nftAuction.connect(user1).endAuction(auctionId);
    })


});

